<?php

namespace app\admin\controller;

use app\admin\model\Good as ModelGood;
use app\common\LogService;
use think\facade\Db;

class Good extends Base
{
    public function index()
    {
        return view();
    }
    public function list()
    {
        $list = ModelGood::list();
        return success($list);
    }
    public function add()
    {
        return view('add');
    }
    public function doAdd()
    {
        $param = input();
        $recordLog = function ($status, $message = '') {
            LogService::record(
                name: '添加商品',
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $param['images'] = checkImg($param['images']);
        $res = ModelGood::create($param);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
    public function edit($id)
    {
        $data = ModelGood::find($id);
        return view('edit', ['data' => $data]);
    }
    public function doEdit()
    {
        $param = input();
        $images = checkImg($param['images']) ?? '';
        $param['images'] = $images;
        $id = $param['id'];
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '编辑商品',
                user_id: $this->uid,
                target_id: $id,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $res = ModelGood::update($param);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '删除商品',
                user_id: $this->uid,
                target_id: $id,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: $this->role

            );
        };
        $res = ModelGood::destroy($id);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
    public function uploadImg()
    {
        $file =  request()->file();
        if (!$file) {
            return error('请上传jpg,png,jpeg后缀图片');
        }
        try {
            validate(['file' => 'fileSize:5242880|fileExt:jpg,png,jpeg'])
                ->check($file);
            if (empty($file)) return error();
            $images = \think\facade\Filesystem::disk('images')->putFile('article', $file['file']);
            if (!$images) return error('图片上传失败');
            $images = '/static/images/' . str_replace('\\', '/', $images);
            Db::name('files')->insert([
                'user_id' => $this->uid,
                'desc' => '商品图片上传',
                'route_path' => request()->controller() . '/' . request()->action(),
                'file_name' => $file['file']->getFilename(),
                'file_path' => $images,
                'size' => $file['file']->getSize(),
                'create_time' => time()
            ]);
            return success(['src' => addHost($images)]);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getMessage());
        }
        LogService::record(
            user_id: getAdminSessionVal(),
            name: '上传商品图片',
        );
    }
}
